﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weiz.TaskManager.DBUtility;
using Weiz.TaskManager.Models;

namespace Weiz.TaskManager.DAL
{
   public class ProductDAL
   {
       /// <summary>
       /// 添加一个新商品
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
       public bool Add(Product model,out string msg)
       {
           msg = "ok";
           try
           {
               var sql = @"INSERT INTO [Product]
           ([ID]
           ,[Name]
           ,[SpecificationsID]
           ,[Price]
           ,[Qty]
           ,[CreateDate])
     VALUES
           (@ID
           ,@Name
           ,@SpecificationsID
           ,@Price
           ,@Qty
           ,getdate())";
               object param = new
               {
                   ID = model.ID,
                   Name = model.Name,
                   SpecificationsID = model.SpecificationsID,
                   Price = model.Price,
                   Qty = model.Qty                
               };
               return SQLHelper.ExecuteNonQuery(sql, param) > 0;
           }
           catch (Exception ex)
           {
               msg = "增加商品异常";
               return false;
           }
         
 
       }


       /// <summary>
       /// 根据商品名称或ID查找商品是否存在
       /// </summary>
       /// <param name="taskId"></param>
       /// <returns></returns>
       public Product GetProductById(string id)
       {
           var sql = @"SELECT [ID]
      ,[Name]
      ,[SpecificationsID]
      ,[Price]
      ,[Qty]
      ,[CreateDate]
  FROM [Product] where ID=@id";

           object param = new { id = id };
           var result = SQLHelper.Single<Product>(sql, param);
           return result;
       }
       /// <summary>
       /// 根据商品名称或ID查找商品是否存在
       /// </summary>
       /// <param name="taskId"></param>
       /// <returns></returns>
       public Product GetProductByName(string name)
       {
           var sql = @"SELECT [ID]
      ,[Name]
      ,[SpecificationsID]
      ,[Price]
      ,[Qty]
      ,[CreateDate]
  FROM [Product] where name=@name";

           object param = new { name = name };
           var result = SQLHelper.Single<Product>(sql, param);
           return result;
       }

   }
}
